package hdd.serivce.impl;

import POJO.Test;
import POJO.User;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.support.JmsUtils;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Component
public class TestServiceImpl implements service.TestService {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public Test gesTest() {
        Test test = new Test();
        System.out.println( "method excuted" );
        List<User> objectList = sqlSessionTemplate.selectList( "User.selectList" );
        testMQQueue();
        testMQTopic();
        return test;
    }

    @Override
    public void testMQQueue() {
        //发送
        jmsTemplate.convertAndSend( "hdd.queue"," 测试  jms" );

        //接收
        ActiveMQTextMessage message = (ActiveMQTextMessage)jmsTemplate.receive( "hdd.queue" );
        try {
            String messageContent =  message.getText();
            System.out.println(messageContent);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void testMQTopic() {
        User user = new User();
        user.setUsername( "test Object" );

        //发送
        jmsTemplate.convertAndSend( "hdd.topic",user );
        //接收
        ActiveMQObjectMessage message = (ActiveMQObjectMessage)jmsTemplate.receive( "hdd.topic" );
        try {
            User messageContent =  (User) message.getObject();
            System.out.println(messageContent);
        } catch (JMSException e) {
            throw  JmsUtils.convertJmsAccessException( e );
        }
    }
}
